/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.networkvm;

import java.io.IOException;

import com.argot.TypeException;
import com.argot.TypeInputStream;
import com.argot.TypeMap;
import com.argot.TypeOutputStream;
import com.argot.network.TypeEndPoint;
import com.argot.network.TypeLink;
import com.argot.remote.MetaLocation;
import com.argot.remote.MetaObject;

import com.colony.networkvm.machine.NvmInstance;
import com.colony.networkvm.machine.NvmState;
import com.colony.remote.RemoteException;


public class NvmHostServer
implements TypeLink, INvmHost
{
	TypeMap _reference;
	NvmEnvironment _environment;

    public NvmHostServer( NvmEnvironment environment, TypeMap reference ) 
    {
        _environment = environment;
        _reference = reference;   
    }

    public NvmState process( NvmState nvmState ) 
    throws RemoteException 
    {
        NvmInstance instance = _environment.getNvmInstance(nvmState);
        _environment.executeInstance( instance );
        return instance.getState();
    }
    
    public MetaObject getObject( MetaLocation location ) 
    throws RemoteException 
    {
    	Object o = _environment.getHostContainer().getObject( location );
    	if ( o == null )
    	{
    		throw new RemoteException("Location not found");
    	}
    	
    	try
		{
			return new MetaObject( location, _reference.getLibrary().getId( o.getClass() ));
		}
		catch (TypeException e)
		{
			throw new RemoteException("No type id for object: " + o.getClass().getName(),e);
		}
    }
 
    // TODO Any failures to read or write the state of the NVM will result
    //      in the client no getting a proper response.  The connection will
    //      be closed.
	public void processMessage( TypeEndPoint connection ) 
	throws IOException
	{
			
		NvmState state = null;
		try
		{
			TypeInputStream sin = new TypeInputStream( connection.getInputStream(), _reference );
			Object o = sin.readObject( NvmState.TYPENAME );
			state = (NvmState) o;
			
		}
		catch (TypeException e)
		{
			e.printStackTrace();
			throw new IOException( "failed to read NVM");
		}
		catch (IOException e)
		{
			e.printStackTrace();
			throw new IOException( "failed to read NVM" );
		}
		catch (Throwable e )
		{
			e.printStackTrace();
			throw new IOException( "failed to read NVM" );
		}
		
        NvmInstance instance;
        try
        {
            instance = _environment.getNvmInstance(state);
            _environment.executeInstance( instance );
            state = instance.getState();
        }
        catch (Throwable e2)
        {
            state.setException( e2 );
        }

		try
		{
			TypeOutputStream tmos = new TypeOutputStream( connection.getOutputStream(), _reference );
			tmos.writeObject( NvmState.TYPENAME, state  );
		}
		catch (TypeException e1)
		{
			e1.printStackTrace();
			throw new IOException( "failed to write NVM" );
		}
		catch (IOException e1)
		{
			e1.printStackTrace();
			throw new IOException( "failed to write NVM" );
		}
		


	}



}
